<!DOCTYPE html>
<html>

{include file='public/header'}

<body>
    <div class="x-body">
        <div class="layui-row">
            <form class="layui-form">
                <div class="layui-inline">
                    <label class="layui-form-label w-auto">平台类型：</label>
                    <div id="pMenu"></div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label w-auto">名称：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label w-auto">充值类型：</label>
                    <div id="pRechargeType" style="width:100%;flex:1"></div>
                </div>
                <button class="layui-btn" style="margin-left: 10px;" lay-submit lay-filter="searchList">搜索</button>
                <button type="reset" id="resetList" class="layui-btn layui-btn-primary">重置</button>
            </form>
        </div>
        <table id="set_list" lay-filter="set_list"></table>
    </div>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="batchChoose">批量选择</button>
        </div>
    </script>
    <script type="text/html" id="tbBar">
        <a class="layui-btn layui-btn-xs" lay-event="choose">选择</a>
    </script>
    <script>
        let PlatformType = new Promise((resolve) => {
            Ajax('/cps/recharge/getPlatformType', {}).then(result => {
                resolve(result.data)
            });
        })
        let getRechargeType = new Promise((resolve) => {
            Ajax('/cps/recharge/getRechargeType', {type:1}).then(result => {
                resolve(result.data)
            });
        })

        Promise.all([PlatformType,getRechargeType]).then((dataArray) => {
            let params = {
                elem: '#set_list'
                , toolbar: '#toolbarDemo'
                , url: '/cps/recharge/rechargeSetList' //数据接口
                , id: 'set_list'
                , page: true
                , limit: 100
                , height:400
                , cols: [[ //表头
                    { type: 'checkbox' },
                    { field: 'id', title: 'ID', width: 60, sort: true }
                    , { field: 'name', title: '名称', }
                    , { field: 'type', title: '充值类型', templet:function(d){

                        for (let index = 0; index < dataArray[1].length; index++) {
                            const element = dataArray[1][index];
                            if(element.id == d.type){
                                return element.name
                            }
                        }

                    }}
                    , { field: 'original_currency', sort: true,title: '充值K币',width:100 }
                    , { field: 'recharge_price',sort: true,  title: '充值金额',width:102 }
                    , { field: 'send_currency', sort: true,title: '赠送K币',width:100 }
                    , { field: 'sum_currency', sort: true,title: '总共K币',width:100 }
                    , { field: 'send_title', title: '赠送提示', }
                    , { field: 'label', title: '标签'}
                    , {
                        field: 'platform_type', title: '使用平台', templet: function (d) {
                            for (let index = 0; index < dataArray[0].length; index++) {
                                const element = dataArray[0][index];
                                if (element.type == d.platform_type) { return element.name }
                            }
                        }
                    }
                    , { toolbar: '#tbBar', title: '操作' ,width:80 }
                ]]
            }
            tableList(params)
        })

        layui.use(["layer","table"], function () {
            var table = layui.table;
            var form = layui.form;
            let type = GetQueryString('type')
            let seting_index = GetQueryString('seting')
            let template_number = Number(GetQueryString('template_number'))
            let sessionName = type == 1 ? 'recharged_content' : 'not_recharged_content'
            table.on('tool(set_list)', function (obj) {
                var event = obj.event;
                if (event === 'choose') {
                    let arr = JSON.parse(sessionStorage.getItem(sessionName)) ?? []
                    if(seting_index){
                        arr.splice(seting_index,1,obj.data)
                    }else{
                        arr.push(obj.data)
                    }
                    arr.forEach((item,index)=>{
                        arr['indexs'] = index
                        delete arr[index].display_object
                        delete arr[index].platform_type
                    })
                    sessionStorage.setItem(sessionName,JSON.stringify(arr))
                    x_admin_close();
                    // x_admin_father_reload();
                }
            });
            table.on('toolbar(set_list)', function (obj) {
                var event = obj.event;
                if (event === 'batchChoose') {
                    var checkStatus = table.checkStatus('set_list'); //idTest 即为基础参数 id 对应的值
                    if(checkStatus.data.length === 0){
                        layer.msg('请选择后再操作',{icon:5,time:1000});
                    }else{
                        if(checkStatus.data.length + template_number > 6){
                            layer.msg('超过数量限制',{icon:5,time:1000});
                            return;
                        }
                        let arr = JSON.parse(sessionStorage.getItem(sessionName)) ?? []
                        checkStatus.data.forEach((item,index)=>{
                            item['indexs'] = index
                            arr.push(item)
                            delete arr[index].display_object
                            delete arr[index].platform_type
                        })
                        sessionStorage.setItem(sessionName,JSON.stringify(arr))
                        x_admin_close();
                        // x_admin_father_reload();
                    }
                }
            });
        });

        let selectParams = {
            url: '/cps/recharge/getPlatformType',
            elem: '#pMenu',
            selected: [],
            tips: '选择平台类型',
            name: 'platform_type',
            field_id: 'type',
            field_Name: 'name',
        }
        selectN(selectParams)

        let selectRechargeType = {
            url:'/cps/recharge/getRechargeType?type=0',
            selected:[],
            tips:'选择充值类型',
            name: 'type',
            field_id:'id',
            field_Name:'name',
            elem: '#pRechargeType'
        }
        selectN(selectRechargeType)
    </script>
</body>

</html>